package com.platform.service;

import com.platform.dao.ApiSchoolMapper;
import com.platform.entity.BuildingVo;
import com.platform.entity.CityOutputDto;
import com.platform.entity.CityVo;
import com.platform.entity.SchoolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ApiSchoolService {
    @Autowired
    private ApiSchoolMapper schoolDao;

    public List<CityOutputDto> queryCityList() {
        List<CityOutputDto> result = new ArrayList<>();
        Map<String,List<CityVo>> cityMap = new HashMap<>();
        List<CityVo> cityList = schoolDao.queryCityList();

        for (CityVo cityVo : cityList) {
            //判断cityMap keys是否存在该城市拼音首字母code
            if (cityMap.containsKey(cityVo.getCode())){
                //存在，将该城市添加到对应的list中
                cityMap.get(cityVo.getCode()).add(cityVo);

            }else {
                //不存在，put城市拼音首字母code对应的key和城市list
                List<CityVo> cityVoList = new ArrayList<>();
                cityVoList.add(cityVo);
                cityMap.put(cityVo.getCode(),cityVoList);
            }
        }
        for (String s : cityMap.keySet()) {
            CityOutputDto cityOutputDto = new CityOutputDto();
            cityOutputDto.setCode(s);
            cityOutputDto.setCityList(cityMap.get(s));
            result.add(cityOutputDto);
        }

        return result;
    }

    public List<SchoolVo> querySchoolList(Integer cityId) {
       return schoolDao.querySchoolList(cityId);
    }

    public List<BuildingVo> queryBuildings(Integer schoolId) {
        return schoolDao.queryBuildings(schoolId);
    }

    public List<SchoolVo> queryLikeName(String schoolName) {
        return schoolDao.queryLikeName(schoolName);
    }

}
